package com.wingsfrog.travel.service.impl;

import com.wingsfrog.travel.modules.dao.UserDao;
import com.wingsfrog.travel.modules.entity.User;
import com.wingsfrog.travel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Created by rankin on 16/3/31.
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserDao userDao;

    @Override
    public void saveUser(User user){
        userDao.save(user);
    }

    @Override
    public void saveUserInfo(Integer userId, String brif, Integer yearsold, Byte sex) throws Exception {
        User u = userDao.findOne(userId);
        if (u == null) throw new Exception("not data");

        if (brif!=null&&!brif.equals(""))u.setBrif(brif);
        if (yearsold!=null&&!yearsold.equals(0))u.setYearsold(yearsold);
        if (sex!=null) u.setSex(sex);
        userDao.save(u);
    }

    @Override
    public User findUserByToken(String token){
        return userDao.getByToken(token);
    }

    @Override
    public User findUserByPhone(String phone) throws Exception {
        if (phone==null&&phone.equals(""))throw new Exception("error phone");
        return userDao.getByPhone(phone);
    }
}
